package com.qf.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsUserMapper;
import com.qf.mapper.StatMapper;
import com.qf.pojo.DtsUser;
import com.qf.service.DtsUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author bss
 * @version V1.0
 * @Project maven
 * @Package com.qf.service.impl
 * @Description:
 * @Date 2022/10/13 20:50
 */
@Service
public class DtsUserServiceImpl implements DtsUserService {

    @Autowired
    DtsUserMapper userMapper;
    @Autowired
    StatMapper statMapper;

    @Override
    public long getTotal() {
        LambdaQueryWrapper<DtsUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsUser::getDeleted,false);
        Integer count = userMapper.selectCount(wrapper);
        return count;
    }

    @Override
    public Map<String, Object> findByPage(Integer page, Integer limit, String sort, String order, String username,String mobile) {
        IPage<DtsUser> iPage = new Page(page,limit);

        QueryWrapper<DtsUser> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(username)){
            wrapper.like("username",username);
        }
        if (!StringUtils.isEmpty(mobile)){
            wrapper.like("mobile",mobile);
        }
        if ("desc".equalsIgnoreCase(order)){
            wrapper.orderByDesc();
        }else {
            wrapper.orderByAsc();
        }

        iPage = userMapper.selectPage(iPage,wrapper);
//        List<DtsUser> userList = iPage.getRecords();
//        List<DtsUser> collect = userList.stream().map(user -> {
//            return user;
//        }).collect(Collectors.toList());

        Map<String, Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("items", iPage.getRecords());
        return map;
    }


    @Override
    public Map<String, Object> selectUserStat() {

//        获取一个月前的今天
        Calendar calendar = Calendar.getInstance();

        calendar.add(Calendar.MONTH,-1);

        Date date = calendar.getTime();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        String format = sdf.format(date);
        List<Map> rows = statMapper.findUserStatis(format);
        String[]  columns= {"day","users"};

//        StatVo statVo = new StatVo();
//        statVo.setRows(rows);
//        statVo.setColumns(columns);

        Map<String, Object> map = new HashMap<>();
        map.put("columns",columns);
        map.put("rows",rows);

        return map;
    }

}
